package huixue.cn.com.rxapp.kchart;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import huixue.cn.com.library.util.PixelUtils;
import huixue.cn.com.rxapp.kchart.adapter.IAdapter;
import huixue.cn.com.rxapp.kchart.view.ScrollAndScaleView;

/**
 * Created by gongdongyang 2019/7/23
 * Describe:
 */
public class BaseKChartView extends ScrollAndScaleView {

    private Rect mMainRect;
    private Rect mTabRect;
    private Rect mChildRect;

    private int mRightPadding;
    private int displayHeight;
    private int mTopPadding;
    private int mBottomPadding;

    private int mWidth;
    private int mHeight;

    private int mMainHeight;
    private int mMainWidth;

    private int mGridRows;
    private int mGridCols;

    private Paint mGridPaint;

    private int mItemCount;//当前点的个数

    private IAdapter mAdapter;

    private int mPointWidth;

    private int mDataLen;

    private int mOverScrollRange = 0;

    private int mScrollX = 0;

    public BaseKChartView(Context context) {
        this(context,null);
    }

    public BaseKChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BaseKChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mRightPadding = PixelUtils.dp2px(60);
        mTopPadding = PixelUtils.dp2px(20);
        mBottomPadding = PixelUtils.dp2px(20);

        mGridRows = 5;
        mGridCols = 4;

        mGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mGridPaint.setStrokeWidth(PixelUtils.dp2px(1));
        mGridPaint.setColor(Color.parseColor("#d0d0d0"));

        mPointWidth = PixelUtils.dp2px(6);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.scale(1,1);
        drawGird(canvas);
        canvas.restore();
    }

    private void drawGird(Canvas canvas) {
       //横向的grid
       float  rowSpace = mMainRect.height()/mGridRows;
       for (int i = 0; i <= mGridRows; i++) {
            canvas.drawLine(0,mTopPadding+rowSpace*i,mMainWidth,mTopPadding+rowSpace*i,mGridPaint);
       }

       //纵向的grid
       float  colSpace = mMainRect.width()/mGridCols;
       for (int i = 0; i <= mGridCols; i++) {
            canvas.drawLine(colSpace*i,mTopPadding,colSpace*i,mHeight-mBottomPadding,mGridPaint);
       }

        canvas.drawLine(0,mHeight-mBottomPadding,mMainWidth,mHeight-mBottomPadding,mGridPaint);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.mWidth = w;
        this.mHeight = h;
        this.mMainWidth = w -mRightPadding;
        initRect(w, h);

    }

    private void initRect(int w, int h) {
        displayHeight = mHeight - mTopPadding - mBottomPadding;
        mMainHeight = (int) (displayHeight * 0.75f);

        mMainRect = new Rect(0, mTopPadding, mWidth-mRightPadding, mTopPadding + mMainHeight);
        mChildRect = new Rect(0, mTopPadding + mMainHeight, mWidth, mHeight-mBottomPadding);
    }


    private void setAdapter(IAdapter adapter){
        if (mAdapter != null && mDataSetObserver != null) {
            mAdapter.unregisterDataSetObserver(mDataSetObserver);
        }
        mAdapter = adapter;
        if (mAdapter != null) {
            mAdapter.registerDataSetObserver(mDataSetObserver);
            mItemCount = mAdapter.getCount();
        }else{
            mItemCount = 0;
        }
        notifyChanged();
    }


    DataSetObserver mDataSetObserver = new DataSetObserver() {
        @Override
        public void onChanged() {
            mItemCount = getAdapter().getCount();
            notifyChanged();
        }

        @Override
        public void onInvalidated() {
            mItemCount = getAdapter().getCount();
            notifyChanged();
        }
    };

    /**
     * 重新计算并刷新线条
     */
    public void notifyChanged() {
        if (mItemCount != 0) {
            mDataLen = (mItemCount - 1) * mPointWidth;
        }else{
            setScrollX(0);
        }
    }

    /**
     * 获取适配器
     *
     * @return
     */
    public IAdapter getAdapter() {
        return mAdapter;
    }

    /**
     * 设置ScrollX
     *
     * @param scrollX
     */
    public void setScrollX(int scrollX) {
        this.mScrollX = scrollX;
        scrollTo(scrollX, 0);
    }

}
